package com.thoughtworks.options;

import com.thoughtworks.domain.Book;

import java.util.ArrayList;
import java.util.List;

public class Library{
    private List<Book> books;

    public Library(ArrayList<Book> books) {
        this.books = books;
    }

    public String listAvailableBooks() {
        String booksString = "";

        for(int index = 0; index < books.size(); index++) {
            Book book = books.get(index);
            if(book.isAvailable()) {
                booksString += index + " | " + book.getInfo() + "\n";
            }
        }

        return booksString.trim();
    }

    public String checkoutABook(int bookIndex) {
        if(bookIndex < books.size() && books.get(bookIndex).isAvailable()) {
            books.get(bookIndex).setIsAvailable(false);
            return "Thank you! Enjoy the book";
        }
        return "That book is not available.";
    }

    public List<Book> getBooks() {
        return books;
    }

    public String returnABook(int bookIndex) {
        if(bookIndex < books.size() && !books.get(bookIndex).isAvailable()) {
            books.get(bookIndex).setIsAvailable(true);
            return "Thank you for returning the book.";
        }
        return "That is not a valid book to return.";
    }
}
